package me.sealer.ssm.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.RgbConstant;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * 功能描述： $ TokenImgGenerator
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月02日 10时29分00秒
 */
@Slf4j
public class TokenImageBytesGenerator {
    private static java.security.SecureRandom generator = new java.security.SecureRandom();

    public TokenImageBytesGenerator() {
    }

    private static Color getRandColor(int i, int j) {
        java.security.SecureRandom random = new java.security.SecureRandom();
        if (i > RgbConstant.MAX_COLOR_VALUE) {
            i = RgbConstant.MAX_COLOR_VALUE;
        }
        if (j > RgbConstant.MAX_COLOR_VALUE) {
            j = RgbConstant.MAX_COLOR_VALUE;
        }
        int k = i + random.nextInt(j - i);
        int l = i + random.nextInt(j - i);
        int i1 = i + random.nextInt(j - i);
        return new Color(k, l, i1);
    }

    private static void shear(Graphics g, int w1, int h1, Color color) {
        shearX(g, w1, h1, color);
        shearY(g, w1, h1, color);
    }

    private static void shearX(Graphics g, int w1, int h1, Color color) {
        int period = generator.nextInt(3);

//    	  boolean borderGap = true;
        int frames = 1;

        int phase = generator.nextInt(200);

        for (int i = 0; i < h1; i++) {
            double d = (double) (period >> 1)
                    * Math.sin((double) i / (double) period
                    + (6.2831853071795862d * (double) phase)
                    / (double) frames);
            g.copyArea(0, i, w1, 1, (int) d, 0);
//    	   if (borderGap) {
            g.setColor(color);
            g.drawLine((int) d, i, 0, i);
            g.drawLine((int) d + w1, i, w1, i);
//    	   }
        }
    }

    private static void shearY(Graphics g, int w1, int h1, Color color) {
        // 50;
        int period = generator.nextInt(1);

//    	  boolean borderGap = true;
        int frames = 20;
        int phase = 7;
        for (int i = 0; i < w1; i++) {
            double d = (double) (period >> 1)
                    * Math.sin((double) i / (double) period
                    + (6.2831853071795862d * (double) phase)
                    / (double) frames);
            g.copyArea(i, 0, 1, h1, 0, (int) d);
//    	   if (borderGap) {
            g.setColor(color);
            g.drawLine(i, (int) d, i, 0);
            g.drawLine(i, (int) d + h1, i, h1);
//    	   }
        }
    }

    public static byte[] createPic(String s) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int i = 15 * s.length() + 20;
            byte byte0 = 30;
            BufferedImage bufferedimage = new BufferedImage(i, byte0, 1);
            Graphics g = bufferedimage.getGraphics();
            java.security.SecureRandom random = new java.security.SecureRandom();
            g.setColor(new Color(217, 217, 255));
            g.fillRect(0, 0, i, byte0);
            g.setFont(new Font("Comic San MS", 0, 21));
            g.setColor(getRandColor(160, 200));
            for (int j = 0; j < 20; j++) {
                int l = random.nextInt(i);
                int i1 = random.nextInt(byte0);
                int j1 = random.nextInt(12);
                int k1 = random.nextInt(12);
                g.drawOval(l, i1, l + j1, i1 + k1);
            }

            for (int k = 0; k < s.length(); k++) {
                char c = s.charAt(k);
                String s1 = String.valueOf(c);
                g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
                g.drawString(s1, 15 * k + 6, 20);
            }
            shear(g, bufferedimage.getWidth(), bufferedimage.getHeight(), new Color(217, 217, 255));

            g.dispose();
            JPEGImageEncoder jpegimageencoder = JPEGCodec.createJPEGEncoder(outputStream);
            jpegimageencoder.encode(bufferedimage);
        } catch (Exception exception) {
            log.error("The token's pic generate error.", exception);
        }

        return outputStream.toByteArray();
    }
}
